require_relative "packages-groonga-org-package-task"

class GroongaPackageTask < PackagesGroongaOrgPackageTask
  def initialize
    super("groonga", detect_version, detect_release_time)
    @archive_name = "groonga-#{@version}.tar.gz"
    @original_archive_name = @archive_name
  end

  private
  def detect_version
    version_env = ENV["VERSION"]
    return version_env if version_env

    base_version_file = File.join(__dir__,
                                  "..",
                                  "base_version")
    version = File.read(base_version_file).chomp
  end

  def detect_target_apache_arrow_version
    latest_release_url = "https://api.github.com/repos/apache/arrow/releases/latest"
    headers = {}
    gh_token = ENV["GH_TOKEN"]
    headers["Authorization"] = "Bearer #{gh_token}" if gh_token
    # e.g: apache-arrow-19.0.0
    latest_tag_name = URI.open(latest_release_url, headers) do |latest_release|
      JSON.parse(latest_release.read)["tag_name"]
    end
    # apache-arrow-19.0.0 -> 19.0.0
    latest_tag_name[/\Aapache-arrow-([\d.]+)\z/, 1]
  end

  def detect_target_apache_arrow_so_version
    major, minor, = detect_target_apache_arrow_version.split(".", 3)
    (major.to_i * 100 + minor.to_i).to_s
  end

  def define_archive_task
    file "../#{@original_archive_name}" do
      source_archive_url = built_package_url(:source, @archive_name)
      download(source_archive_url, "..")
    end

    file @original_archive_name => "../#{@original_archive_name}" do
      ln_s("../#{@original_archive_name}",
           @original_archive_name)
    end
  end

  def docker_build_options(os, architecture)
    [
      "--build-arg",
      "APACHE_ARROW_VERSION=#{detect_target_apache_arrow_version}",
    ]
  end

  def docker_run_options(os, architecture)
    if os.start_with?("ubuntu-")
      [
        "--env", "DEB_EMULATE_LAUNCHPAD_ENVIRONMENT=yes",
      ]
    else
      []
    end
  end

  def apt_targets_default
    [
      "debian-bookworm",
      "debian-bookworm-arm64",
      "debian-trixie",
      "debian-trixie-arm64",
      "ubuntu-jammy",
      "ubuntu-jammy-arm64",
      "ubuntu-noble",
      "ubuntu-noble-arm64",
    ]
  end

  def apt_prepare_debian_control_faiss(control, target)
    if target.start_with?("ubuntu-noble")
      use_bundled_faiss = ""
      use_system_faiss = "#"
    else
      use_bundled_faiss = "#"
      use_system_faiss = ""
    end
    control.
      gsub(/@USE_BUNDLED_FAISS@/, use_bundled_faiss).
      gsub(/@USE_SYSTEM_FAISS@/, use_system_faiss)
  end

  def apt_prepare_debian_control(control_in, target)
    control = control_in.dup
    control = apt_prepare_debian_control_faiss(control, target)
    control
  end

  def yum_targets_default
    [
      "almalinux-8",
      "almalinux-8-aarch64",
      "almalinux-9",
      "almalinux-9-aarch64",
      "almalinux-10",
      "almalinux-10-aarch64",
      "amazon-linux-2023",
      "amazon-linux-2023-aarch64",
    ]
  end

  def yum_expand_variable(key)
    case key
    when "APACHE_ARROW_VERSION"
      detect_target_apache_arrow_version
    when "APACHE_ARROW_SO_VERSION"
      detect_target_apache_arrow_so_version
    else
      super
    end
  end

  def source_targets_default
    [
      "#{@archive_base_name}.tar.gz",
      "#{@archive_base_name}.zip",
    ]
  end

  def windows_targets_default
    [
      "#{@package}-#{@version}-x64-vs2022.zip",
      "#{@package}-#{@version}-x64-vs2022-with-vcruntime.zip",
    ]
  end

  def use_built_package?
    true
  end

  def github_repository
    ENV["GITHUB_REPOSITORY"] || "groonga/groonga"
  end

  def github_actions_workflow_file_name(target_namespace, target)
    case target_namespace
    when :apt, :yum
      "package.yml"
    when :windows
      "windows-msvc.yml"
    else
      super
    end
  end

  def github_actions_artifact_name(target_namespace, target)
    case target_namespace
    when :apt
      if target.end_with?("-i386") or target.end_with?("-arm64")
        "packages-#{target}"
      else
        "packages-#{target}-amd64"
      end
    when :yum
      if target.end_with?("-aarch64")
        "packages-#{target}"
      else
        "packages-#{target}-x86_64"
      end
    else
      raise NotImplementedError
    end
  end

  def built_package_url(target_namespace, target)
    case target_namespace
    when :apt
      if target.end_with?("-i386") or target.end_with?("-arm64")
        base_name = "#{target}.tar.gz"
      else
        base_name = "#{target}-amd64.tar.gz"
      end
    when :yum
      if target.end_with?("-aarch64")
        base_name = "#{target}.tar.gz"
      else
        base_name = "#{target}-x86_64.tar.gz"
      end
    else
      base_name = target
    end
    github_download_url(base_name)
  end

  def built_package_n_split_components
    3
  end

  def download_packages(target_namespace)
    super unless target_namespace == :windows
  end

  def tag_name
    "v#{@version}"
  end
end

task = GroongaPackageTask.new
task.define
